package ercha;
import ercha.root;

import java.util.*;

class root {
    private String val;
    public root left;
    public root right;

    public root(String a) {
        this.val = a;
        this.left=null;
        this.right=null;
    }
}
    public class tset {
    public static root quee(){
        root a = new root("A");
        root b = new root("B");
        root c = new root("C");
        root d = new root("D");
        root e = new root("E");
        root f = new root("F");
        root g = new root("G");

        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        c.left = f;
        c.right = g;
        return a;
    }
        private static int size(root root1) {
            if (root1 == null)
                return 0;
            return 1 + size(root1.left) + size(root1.right);
        }
        private static int getLeafSize(root root1) {
            if (root1 == null)
                return 0;
            if(root1.left==null&&root1.right==null) {
                return 1;
            }
            return  getLeafSize(root1.left) + getLeafSize(root1.right);
        }
        //求二叉树第k层节点的个数
        private static int getKLevelNodeCount(root root1,int k){
            if(k<1) return 0;
            if(k==1) return 1;
            return getKLevelNodeCount(root1.left,k-1)+getKLevelNodeCount(root1.right,k-1);
                //throw new RuntimeException();
        }
        //求二叉树的高度
        private static int getHeight(root a){
        if(a==null)return 0;
//        root left=a.left;
//        root right=a.right;
            return Math.max(getHeight(a.left),getHeight(a.right))+1;
        }
        //判断是不是完全二叉数
        private static boolean cenxu(root a){
        if(a==null) return true;
            //要进行层序遍历如何符合条件就继续递归如果不符合条件就进行第二条件的判断
            //我们可以用一个队列来进行层序遍历达到出队列和入队列的效果
            //判断的第一阶段为该节点为null 或着只有左子树 只有有字数直接返还flase
            boolean df=true;
            Queue<root> arr=new LinkedList<>();
            arr.offer(a);
            //System.out.println(arr.isEmpty());
            while(!arr.isEmpty()){
                root vb=arr.poll();
                if(df){
                    if(vb.left==null&&vb.right==null){
                        df=false;
                    }
                    else if(vb.left!=null&&vb.right==null){
                    df=false;
                    arr.offer(vb.left);
                    }
                    else if(vb.left==null&&vb.right!=null){
//                        arr.offer(a.left);
//                        df=false;
                        return false;
                    } else {
                        arr.offer(vb.left);
                        arr.offer(vb.right);
                    }
                }else{
                    if(vb.left!=null&&vb.right!=null)
                    return false;
                }
            }
            return true;
        }

        public static void main(String[] args) {
        //ArrayList <Integer> arr=new ArrayList<>();
        //int [] arr=new arr[];
            root s=quee();
//            System.out.println(size(s));
//            System.out.println(getLeafSize(s));
//            System.out.println(getKLevelNodeCount(s,3));
            //System.out.println(getHeight(s));
            System.out.println(cenxu(s));
            Queue<Integer> arr1=new LinkedList<>();
            //arr1.offer(5);
            //System.out.println(arr1.isEmpty());
        }
    }

